<?php
class AjaxController extends Zendvn_Controller_Action {
	
	protected $_arrParam; //Mang tham so nhan duoc khi mot Action chay
	protected $_currentController; //Duong dan cua Controller
	protected $_actionMain; //Duong dan cua Action chinh
	
	//Thong so phan trang
	protected $_paginator = array(
									'itemCountPerPage' => 15,
									'pageRange' => 10,
									'currentPage' => 1
							);
	protected $_namespace;
	
	public function init(){
		
		$this->_arrParam = $this->_request->getParams(); //Mang tham so nhan duoc khi mot Action chay
		$this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller']; //Duong dan cua Controller
		$this->_actionMain = '/' . $this->_arrParam['module'] . '/'	. $this->_arrParam['controller'] . '/index'; //Duong dan cua Action chinh
		
		//Luu cac du lieu filter vaof SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		
		//Lay thong tin so phan tu tren mot trang
		if(isset($this->_arrParam['limitPage'])){
			$ssFilter->limitPage = $this->_request->getParam('limitPage');
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}elseif(!empty($ssFilter->limitPage)){
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}
		
		//Trang hien tai
		if(isset($this->_arrParam['page'])){
			$this->_paginator['currentPage'] = $this->_arrParam['page'];
			$this->_page = '/page/' . $this->_arrParam['page'];
		}
		
		//Truyen thong tin phan trang vao mang du lieu
		$this->_arrParam['paginator'] = $this->_paginator;
		
		//Truyen ra ngoai view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
	
		$this->view->siteConfig = Zend_Registry::get('siteConfig');
		$template_path = TEMPLATE_PATH . "/public/" . $this->view->siteConfig['template']['site'];
		$this->loadTemplate($template_path, 'template.ini', 'user');
		
		$language = Zend_Registry::get('language');
		$this->view->language = $language['language'];
	}
	
	public function loadCitiesAction() {
		$tblCities = new Default_Model_Cities();
		$this->view->slbCities = $tblCities->itemInSelectbox($this->_arrParam, array('task'=>'get-cities-load'));
		$this->_helper->layout->disableLayout();
	}
		
	public function loadDistrictsAction() {
		$tblDistricts = new Default_Model_Districts();
		$this->view->slbDistricts = $tblDistricts->itemInSelectbox($this->_arrParam, array('task'=>'get-districts-load'));
		$this->_helper->layout->disableLayout();
	}
	
	public function loadManagersAction() {
		$tblNhanVien = new Default_Model_Users();
		$this->view->slbNhanVien = $tblNhanVien->itemInSelectbox($this->_arrParam, array('task' => 'get-nhanvien-load'));
		$this->_helper->layout->disableLayout();
	}
	
	public function savePermissionAction(){
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		if($this->_request->isPost()){
			$tblAjax = new Default_Model_Ajax();
			$tblAjax->saveItem($this->_arrParam, array('task' => 'permission-add'));
		}
	}
	
	public function listUserAction(){
		$this->_helper->layout->disableLayout();
		
		$tblUserGroup 	= new Default_Model_UserGroup();
		$tblUser 		= new Default_Model_Users();
		$tblChucvu		= new Default_Model_Chucvu();
		$tblPhongban	= new Default_Model_Phongban();
		$tblCoso		= new Default_Model_Coso();
		
		$this->view->slbUserGroup 	= $tblUserGroup->itemInSelectbox();
		$this->view->slbChucvu 		= $tblChucvu->itemInSelectbox();
		$this->view->slbPhongban	= $tblPhongban->itemInSelectbox();
		$this->view->slbCoso		= $tblCoso->itemInSelectbox();
		
	}
	
	public function loadUserAction(){
		$this->_arrParam['paginator']['itemCountPerPage'] = 12;
		$this->_paginator['itemCountPerPage'] = 12;
		
		$this->_helper->layout->disableLayout();
		$tblUser 			= new Default_Model_Users();
		$this->view->Users 	= $tblUser->listItem($this->_arrParam, array('task' => 'admin-list'));
		
		$totalItem = $tblUser->countItem($this->_arrParam);
		$paginator = new Zendvn_Paginator();
		$this->view->paginator = $paginator->createPaginator($totalItem, $this->_paginator);
	}
	
	public function ajaxPhonghocAction(){
		$this->_helper->layout->disableLayout();
		
		$tblPhonghoc	= new Daotao_Model_Phonghoc();
		$this->view->Phonghoc = $tblPhonghoc->itemInSelectbox($this->_arrParam);
	}
	
	public function ajaxLichhocchonAction(){
		$this->_helper->layout->disableLayout();
	
		$tblAjax 			= new Default_Model_Ajax();

		$this->view->Items 		= $tblAjax->listItemLichhoc($this->_arrParam, array('task'=>'admin-list-chon'));
	}
	
	public function ajaxLichhocAction(){
		$this->_helper->layout->disableLayout();
		
		$tblAjax 			= new Default_Model_Ajax();
		$tblPhonghoc 		= new Daotao_Model_Phonghoc();
		$tblDiadiemhoc 		= new Daotao_Model_Diadiemhoc();
		
		$this->view->Diadiemhoc = $tblDiadiemhoc->itemInSelectbox();
		$this->view->Phonghoc 	= $tblPhonghoc->itemInSelectbox(array('id_diadiemhoc' => $this->_arrParam['id_diadiemhoc']));
		$this->view->Items 		= $tblAjax->listItemLichhoc($this->_arrParam, array('task'=>'admin-list'));
		
		$totalItem 				= $tblAjax->countItemLichhoc($this->_arrParam, array('task'=>'admin-list'));
		$paginator 				= new Zendvn_Paginator();
		$this->view->paginator 	= $paginator->createPaginator($totalItem, $this->_paginator);
	}
	
	public function ajaxKhachhangAction(){
		$this->_helper->layout->disableLayout();
		
		$tblAjax 			= new Default_Model_Ajax();
		$tblMarketing		= new Marketing_Model_Item();
		
		$this->view->Items	= $tblAjax->listItem($this->_arrParam, array('task' => 'admin-khachhang'));
		$this->view->Trangthai	= $tblMarketing->itemInSelectbox(null, array('task' => 'get-trangthai'));
	}
}





